package com.school.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

/**
 * 
 * 保存文件
 * @author admin
 *
 */
public class FileUploadUtil {

	  public static String saveFileInfo(MultipartFile imgFile,String ProjectPath){
		  
		  ProjectPath=ProjectPath.split("class")[0]+"myimages/schools/";
			//获得原来文件名(含后缀名)
	        String originalFilename = imgFile.getOriginalFilename();
	        int pos = originalFilename.lastIndexOf(".");
	        if(originalFilename==""){
	        	return null;
	        }
	        //原文件后缀名
	        String suffix = originalFilename.substring(pos);
	        //保存文件
	        //ServletContext application = session.getServletContext();
	        //String realPath = application.getRealPath("D:\\workspace111\\ycpolice-web\\web\\static\\updownload");
	        String realPath = ProjectPath;
	        //产生一个uuid随机文件名
	        String uuid = UUID.randomUUID().toString();
	        String fullPath = realPath + File.separator + uuid + suffix;
	        String fileName= uuid + suffix;
	        InputStream in = null;
			try {
			in = imgFile.getInputStream();
			OutputStream out = new FileOutputStream(new File(fullPath));
		
		
			int len = 0;
			byte[] buf = new byte[3 * 1024];
			while ((len = in.read(buf)) != -1) {
			out.write(buf, 0, len);
			}
			out.close();
			in.close();
		
		
			} catch (IOException e) {
			e.printStackTrace();
			}
			return fileName;
			  }
		
}
